package org.zcz.packing.tool.handler;

import org.zcz.packing.tool.config.ProjectConfig;
import org.zcz.packing.tool.util.PackageUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangcz
 * @title: JavaFileHandler
 * @projectName svnpackageutil
 * @description: TODO
 * @date 2019/6/1217:20
 */
public class JavaFileHandler implements HandlerInterface{
    public void handler(List<Map<String, String>> copyList, String svnPath, ProjectConfig config, String packagePath) {
        String sourcePath = config.getProjectPath() + File.separator + BYTTER_BUILD + File.separator + WEB_INF + File.separator + CLASSES;
        String packageFilePath = packagePath + File.separator + WEB + File.separator + WEB_INF + File.separator + CLASSES;
        String classPath = svnPath.split("java")[1];
//        String fileName = classPath+"class";
//        String _sourcePath = sourcePath + fileName;
//        String _packageFilePath = packageFilePath + fileName;
//        pathMap.put(PackageAction.SOURCE,_sourcePath);
//        pathMap.put(PackageAction.DEST,_packageFilePath);
        classPath = classPath.substring(0,classPath.length()-1);

        int index = classPath.lastIndexOf("/");
        String classPathTemp = classPath.substring(0,index);
        String sourcetempPath = sourcePath + classPathTemp;
        String targetFileName = classPath.substring(index+1,classPath.length());
        File sourceFile = new File(sourcetempPath);
        File[] files = sourceFile.listFiles();
        for (int i=0;i<files.length;i++){
            File file = files[i];
            String neededName = file.getName();
            if(neededName.endsWith("class") && neededName.startsWith(targetFileName)){
                Map<String,String> pathMap = new HashMap<String, String>();
                pathMap.put(PackageUtil.SOURCE,file.getAbsolutePath());
                pathMap.put(PackageUtil.DEST,packageFilePath+classPathTemp+"/"+neededName);
                copyList.add(pathMap);
            }
        }
    }
}
